package com.liang.hellocompose.net

import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * 自定义拦截器
 */
class CustomInterceptor : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        return chain.proceed(request = request)
        /*var baseUrl = ""
        *//*request.headers.forEach {
            baseUrl = when ("${it.first}:${it.second}") {
                HeaderConst.HEADER_XF -> KVUtil.get(MMKVConst.API_NEW_HOUSE, "")
                HeaderConst.HEADER_ES -> KVUtil.get(MMKVConst.API_SECONDHAND, "")
                HeaderConst.HEADER_PASSPORT -> KVUtil.get(MMKVConst.API_USER_CENTER, "")
                HeaderConst.HEADER_BIZ -> KVUtil.get(MMKVConst.API_EXTRA_BIZ, "")
                HeaderConst.HEADER_TALK -> KVUtil.get(MMKVConst.API_EXTRA_TALK, "")
                HeaderConst.HEADER_TALK_TEST -> "https://talk.centanet.com/im/"
                HeaderConst.HEADER_PASSPORT_JAVA -> KVUtil.get(MMKVConst.API_USER_CENTER_JAVA, "")
                else -> ""
            }
        }*//*
        try {
            val newBaseUrl = baseUrl
            return if (newBaseUrl.isEmpty())
                chain.proceed(createNewRequest(request, false))
            else {
                val urlBuilder = baseUrl.toHttpUrl().newBuilder()
                for (pathSegment in request.url.pathSegments) {
                    urlBuilder.addPathSegment(pathSegment)
                }
                urlBuilder.encodedQuery(request.url.encodedQuery)
                chain.proceed(
                    createNewRequest(
                        request.newBuilder().url(urlBuilder.build()).build()
                    )
                )
            }
        } catch (e: Exception) {
            e.printStackTrace()
            throw e
        }*/
    }

    @Suppress("SpellCheckingInspection")
    private fun createNewRequest(request: Request, needCity: Boolean = true): Request {
        val builder = request.newBuilder()
//        builder.removeHeader(HeaderConst.HEADER_KEY)
//        val cityEn = ZFConfig.cityEn
        /*if ("GET".equals(request.method, true)) {
            val urlBuilder = request.url.newBuilder()
            urlBuilder.addQueryParameter("platform", "android")
            if (needCity && cityEn.isNotEmpty()) {
                urlBuilder.addQueryParameter("cityen", cityEn)
            }
            builder.url(urlBuilder.build())
        } else {
            builder.header("platform", "android")
            if (needCity && cityEn.isNotEmpty()) {
                builder.header("cityen", cityEn)
            }
        }
        builder.header("OSVersion", Build.VERSION.SDK_INT.toString())
            .header("appVersion", ZFConfig.appVersionCode.toString())
            .header("DeviceID", ZFConfig.deviceId)*/
        return builder.build()
    }

}